﻿
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CLVisionTrace;
using System.IO;
using CLVisionHalconWorker;

namespace CLVisionHalcon_NC
{
    public class HalconCallerNC : HalconCaller
    {
       
        public HalconCallerNC(string logPath)
        {
            Trace.SetLogPath(logPath);
        }

        public bool InitHDevEngineNC(out int ErrorCode, out string ErrorText)
        {
            var ret = base.InitHDevEngine(out ErrorCode, out ErrorText);
            if (ErrorCode == -1)
            {
                Trace.Error(ErrorText);
            }
            else
            {
                Trace.Info(ErrorText);
            }
            return ret;
        }

        /// <summary>
        /// 添加Procedure路径
        /// </summary>
        /// <param name="procedurePath">Procedure路径</param>
        /// <param name="ErrorCode">错误代码</param>
        /// <param name="ErrorText">错误信息</param>
        /// <returns></returns>
        public bool AddProcedurePathNC(string procedurePath, out int ErrorCode, out string ErrorText)
        {
            if (base.ProcedurePaths.Contains(procedurePath))
            {
                base.ProcedurePaths.Remove(procedurePath);
            }
            var ret = base.AddProcedurePath(procedurePath, out ErrorCode, out ErrorText);
            if (ErrorCode == -1)
            {
                Trace.Error(ErrorText);
            }
            else
            {
                Trace.Info(ErrorText);
            }
            return ret;
        }
        /// <summary>
        /// 添加Procedure路径数组
        /// </summary>
        /// <param name="procedurePath">Procedure路径</param>
        /// <param name="ErrorCode">错误代码</param>
        /// <param name="ErrorText">错误信息</param>
        /// <returns></returns>
        public bool AddProcedurePathNC(string[] procedurePath, out int ErrorCode, out string ErrorText)
        {
            for (int i = 0; i < procedurePath.Length; i++)
            {
                if (base.ProcedurePaths.Contains(procedurePath[i]))
                {
                    base.ProcedurePaths.Remove(procedurePath[i]);
                }
            }

            var ret = base.AddProcedurePath(procedurePath, out ErrorCode, out ErrorText);
            if (ErrorCode == -1)
            {
                Trace.Error(ErrorText);
            }
            else
            {
                Trace.Info(ErrorText);
            }
            return ret;
        }

        /// <summary>
        /// 加载Procedure
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <returns></returns>
        public bool LoadProcedureNC(string procedureName, out int ErrorCode, out string ErrorText)
        {
            var ret = LoadProcedure(procedureName, out ErrorCode, out ErrorText);
            if (ErrorCode == -1)
            {
                Trace.Error(ErrorText);
            }
            else
            {
                Trace.Info(ErrorText);
            }
            return ret;
        }

        /// <summary>
        /// 调用Procedure，参数以HTuple传递
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="hObj_InputIconic"></param>
        /// <param name="hObj_OutputIconic"></param>
        /// <param name="hTuple_InputCtrl"></param>
        /// <param name="hTuple_OutputCtrl"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <returns></returns>
       // public bool CallProcedureNC(string procedureName, HObject[] hObj_InputIconic, out HObject[] hObj_OutputIconic,
       //     HTuple hTuple_InputCtrl, out HTuple hTuple_OutputCtrl, out int ErrorCode, out string ErrorText)
       // {
       //     var ret = base.CallProcedure(procedureName, hObj_InputIconic, out hObj_OutputIconic,
       //hTuple_InputCtrl, out hTuple_OutputCtrl, out ErrorCode, out ErrorText);

       //     if (ErrorCode == -1)
       //     {
       //         Trace.Error(ErrorText);
       //     }
       //     else
       //     {
       //         Trace.Info(ErrorText);
       //     }
       //     return ret;
       // }
        /// <summary>
        /// 调用Procedure，参数以HTupleVector传递
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="hObj_InputIconic"></param>
        /// <param name="hObj_OutputIconic"></param>
        /// <param name="hTuple_InputCtrl"></param>
        /// <param name="hTuple_OutputCtrl"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <returns></returns>
        public bool CallProcedureNC(string procedureName, HObject[] hObj_InputIconic, out HObject[] hObj_OutputIconic,
           HTupleVector hTuple_InputCtrl, out HTupleVector hTuple_OutputCtrl, out int ErrorCode, out string ErrorText)
        {
            var ret = base.CallProcedure(procedureName, hObj_InputIconic, out hObj_OutputIconic,
       hTuple_InputCtrl, out hTuple_OutputCtrl, out ErrorCode, out ErrorText);

            if (ErrorCode == -1)
            {
                Trace.Error(ErrorText);
            }
            else
            {
                Trace.Info(ErrorText);
            }
            return ret;
        }
        /// <summary>
        /// 获取procedure参数签名
        /// </summary>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <returns></returns>
        public bool GetProcedureParNamesNC(out int ErrorCode, out string ErrorText)
        {
            var ret = base.GetProcedureParNames(out ErrorCode, out ErrorText);
            if (ErrorCode == -1)
            {
                Trace.Error(ErrorText);
            }
            else
            {
                Trace.Info(ErrorText);
            }
            return ret;
        }

        /// <summary>
        /// 获取procedure文件名称
        /// </summary>
        /// <param name="showProcedurePath"></param>
        /// <param name="procedureNames"></param>
        /// <param name="ErrorCode"></param>
        /// <param name="ErrorText"></param>
        /// <param name="searchOption"></param>
        /// <returns></returns>
        public bool GetProcedureNamesNC(string[] showProcedurePath, out string[] procedureNames, out int ErrorCode, out string ErrorText, SearchOption searchOption = SearchOption.AllDirectories)
        {
            var ret = base.GetProcedureNames(showProcedurePath, out procedureNames, out ErrorCode, out ErrorText, searchOption);
            if (ErrorCode == -1)
            {
                Trace.Error(ErrorText);
            }
            else
            {
                Trace.Info(ErrorText);
            }
            return ret;
        }

        /// <summary>
        /// 获取当前在调用的HDevProcedure
        /// </summary>
        /// <returns></returns>
        public HDevProcedure GetHDevProcedureNC()
        {
            return base.RunProcedure;
        }
    }
}
